package com.eric.rabbit.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: eric
 * @create: 2022-03-04 14:04 相同的routeKey，不通的queue
 * 一个生产者 -> 一个exchange -> 两个队列Queue -> 各自对应的消费者
 * exchange -> Queue1( red.# )
 * exchange -> Queue2( red.# )
 *
 * [sameKeyReceive1] Received 'topic:red:0'
 * [sameKeyReceive2] Received 'topic:red:0'
 * 各自消费各自的消息
 */
@Configuration
public class SameRouteKeyConfig {

    private static final String routeKey = "red.#";

    @Bean
    public TopicExchange sameKeyExchange() {
        return new TopicExchange("sameRouteKey.exchange");
    }

    @Bean
    public Queue sameKeyQueue1() {
        return new Queue("queue-1", true);
    }

    @Bean
    public Queue sameKeyQueue2() {
        return new Queue("queue-2", true);
    }

    @Bean
    public Binding sameKeyBind1() {
        //进行exchange与queue绑定
        return BindingBuilder.bind(sameKeyQueue1()).to(sameKeyExchange()).with(routeKey);
    }

    @Bean
    public Binding sameKeyBind2() {
        //进行exchange与queue绑定
        return BindingBuilder.bind(sameKeyQueue2()).to(sameKeyExchange()).with(routeKey);
    }
}
